package com.igeek.ch03summary;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @author xuxin
 * @since 2024/5/22
 */
@Component
public class MyAware implements InstantiationAwareBeanPostProcessor {
    //实例化前
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (beanName.equals("people")) {
            System.out.println("postProcessBeforeInstantiation 实例化前");
        }
        return null;
    }

    //实例化后
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (beanName.equals("people")) {
            System.out.println("postProcessAfterInstantiation 实例化后");
        }
        return true;//注意false代表实例不可使用，true代表实例可以使用,所以改为true
    }

    //初始化前
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof People) {
            System.out.println("postProcessBeforeInitialization 初始化前");
        }
        return bean;//允许返回null，代表实例bean不作处理直接返回使用；但是若实例有@PostConstruct注解的存在，那么返回null会破坏@PostConstruct的存在
    }

    //初始化后
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof People) {
            System.out.println("postProcessAfterInitialization 初始化后");
        }
        return bean;
    }
}
